<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'db_connection.php';
require_once DOCUMENT_ROOT . DIR_SHARED_LIBRARIES . 'lib_date.php';

class dao_sub_menu {

    //Methods
    public function __construct() {
        
    }

    public function get_all() {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_sub_menu sm LEFT JOIN tbl_menu m ON sm.menu_id = m.menu_id";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

    public function add($sub_menu_id, $menu_id, $sub_menu_title, 
            $sub_menu_content, $file_upload, 
            $sub_menu_feature_title, $sub_menu_feature_content) {
        $db = new db_connection();
        $con = $db->open_connect();

        if ($sub_menu_id == 0) {
            
            $query = "INSERT INTO tbl_sub_menu(menu_id, title, content, file_upload, feature_title, feature_content) VALUES (" 
                    . $menu_id . ",'" 
                    . $sub_menu_title . "','" 
                    . $sub_menu_content . "','" 
                    . $file_upload . "','"
                    . $sub_menu_feature_title . "','" 
                    . $sub_menu_feature_content . "')";
        } else {

            $query = "UPDATE tbl_sub_menu SET menu_id = " 
                    . $menu_id . " , title = '" 
                    . $sub_menu_title . "' , content = '" 
                    . $sub_menu_content . "', file_upload = '" . $file_upload . "', feature_title = '" . $sub_menu_feature_title . "', feature_content = '" . $sub_menu_feature_content . "' WHERE sub_menu_id = " . $sub_menu_id;
        }
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function get_by_id($id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbl_sub_menu WHERE sub_menu_id = " . $id;
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $row;
    }

    public function delete($id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "DELETE FROM tbl_sub_menu WHERE sub_menu_id = " . $id;
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }
    
    public function get_all_by_menu_key($menu_key) {
        $db = new db_connection();
        $con = $db->open_connect();
        
        $query = "SELECT * FROM tbl_sub_menu sm JOIN tbl_menu m ON sm.menu_id = m.menu_id WHERE m.menu_key = '" . $menu_key . "'";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }
    
}
